package com.cms.controller;
 

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.LotteryDataSocket;
import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant; 
import com.cms.model.TrAdminUser;
import com.cms.model.TrLotteryDataVo;
import com.cms.model.TrLotteryVo;
import com.cms.service.LotteryDataService;
import com.cms.service.LotteryService;
import com.cms.service.ReportService; 
import com.google.gson.Gson;

@Controller
public class LotteryDataController extends BaseController {
	
	@Autowired
	private LotteryDataService lotteryDataService;
	
	@Autowired
	private LotteryService lotteryService;
	
	@Autowired
	private ReportService reportService;
	
	@RequestMapping("/lotteryData.do")
	public String enter(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
		String lotteryId = this.getActivityCode();// 获取作业的id
		int lottery = StringUtils.str2Int(StringUtils.replace(lotteryId, CmsConstant.LOTTERY_DATACODE, StringUtils.EMPTY));
		TrLotteryVo vo = new TrLotteryVo();
		vo.setId(lottery);
		vo = this.lotteryService.getLotteryInfo(vo);
		map.put("lottery", vo);
		return "lottery/data/lotteryDataMain.html";
	}
	
	
	@RequestMapping("/lotteryData/pagingLotteryData.do")
	public String pagingLotteryData(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
		Map<String,Object>param = this.buildQueryMap(req);
	    map.put("lotteryDataList", this.reportService.pagingReportResult("pagingLotteryData", param));
	    map.put("statisticsData",reportService.querySingleResult("statisticsLotteryData", param));
	    return "lottery/data/lotteryDataList.html";
	}
	
	
	@RequestMapping("/lotteryData/toEditLotteryData.do")
	public String toEditLotteryData(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{ 
	    map.put("lotteryDataList", this.reportService.pagingReportResult("", this.buildQueryMap(req)));
		return "lottery/data/lotteryDataList.html";
	}
	
	@RequestMapping("/lotteryData/toManualDrawing.do")
	// 跳转手动开奖页面
	public String toManualDrawing(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{  
	   map.put("lotteryDataInfo", this.reportService.querySingleResult("queryLotteryDataInfo", this.buildQueryMap(req)));
	   return "lottery/data/lotteryDataManualDrawing.html"; 
	}
	
	
	@RequestMapping("/lotteryData/manualDrawing.do")
	public void manualDrawing(HttpServletRequest req, HttpServletResponse resp, ModelMap map) throws Exception 
	{    
	    TrLotteryDataVo data=new TrLotteryDataVo();
	    TrAdminUser user = super.getLoginUser(req);
	    Map<String,Object>result = new HashMap<String, Object>();
	    try
	    {
	    	data.setModifyUser(user.getId());
	        this.export(req, "lotteryData", data);	
	        Date lotteryDate =DateUtils.parseDate(data.getLotteryTime(), "yyyy-MM-dd HH:mm:ss");
	        if(lotteryDate==null)//当前时间为空
	        {
	        	throw new Exception("彩票的开奖时间为空，无法开彩。");
	        }
	        if(lotteryDate.after(new Date()))
	        {
	        	throw new Exception("该期彩票还未到开彩时间，无法开彩。");
	        }
		    this.lotteryDataService.manualDrawingLottery(data);
		    Thread.sleep(2000);
			int lotteryId = data.getLotteryId();
			String lotteryNo =data.getLotteryMatches();
			new LotteryDataSocket().sendLotteryDataSocket(lotteryId, lotteryNo);
		    result.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
	    }
	    catch (Exception e) {
	    	result.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
	    	result.put(CmsConstant.MSG, e.getMessage());
		}
	    this.renderText(resp, new Gson().toJson(result));
	}
}
